package com.park.controller;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.Permissions;
import com.park.model.SysList;
import com.park.model.UserList;
import com.park.model.UserlistPermissions;
import com.park.service.SysListService;
import com.park.service.UserListService;
import com.park.service.Utility;

@Controller
@RequestMapping("/userList")
public class UserListController {
	@Autowired
	private UserListService userListService;
	@Autowired
	private SysListService sysListService;
	private static Log logger = LogFactory.getLog(UserListController.class);

	/**
	 * 注册用户
	 * 
	 * @param userName
	 * @param password
	 */
	@RequestMapping(value = "/registerUser",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String registerUser(@RequestBody UserList userList) {
		logger.info("[registerUser]参数:"+userList);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		UserList getUserList = userListService.getByUserName(userList.getUserListname());
		if (getUserList != null) {
			resultMap.put("status", 1002);
			resultMap.put("message", "用户已存在");
		} else {
			int count = userListService.insert(userList);
			if (count == 1) {
				resultMap.put("status", 1001);
				resultMap.put("message", "注册成功");
			} else {
				resultMap.put("status", 1002);
				resultMap.put("message", "注册失败");
			}
		}
		logger.info("[registerUser]返回值:"+resultMap);
		return Utility.gson.toJson(resultMap);
	}

	/**
	 * 验证登陆
	 * 
	 * @param userName
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/login",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String login(@RequestBody Map<String,Object> args) {
		logger.info("[login]参数:"+args);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		String userName = (String)args.get("userName");
		String password = (String)args.get("password");
		UserList getUserList = userListService.getByUserName(userName);
		if(getUserList != null) {
		if(password.compareTo(getUserList.getPassword()) == 0) {
			resultMap.put("status", 1001);
			resultMap.put("message", "登录成功");
			resultMap.put("token", userName);
		}else {
			resultMap.put("status", 1002);
			resultMap.put("message", "用户密码不匹配");
		}
		}else {
			resultMap.put("status", 1002);
			resultMap.put("message", "用户密码不匹配");
		}
		return Utility.gson.toJson(resultMap);
	}
	
	@RequestMapping(value = "/getMenu",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getMenu(@RequestBody Map<String,Object> args) {
		Map<String,Object> resultMap = new HashMap<String, Object>();
		String userName = (String)args.get("userName");
		List<SysList> sysList = sysListService.getByName(userName);
		resultMap.put("status", 1001);
		resultMap.put("body", sysList);
		resultMap.put("userName", userName);
		return Utility.gson.toJson(resultMap);
	}
	

	@RequestMapping(value = "/bindingRoles",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String bindingRoles(@RequestBody List<UserlistPermissions> userlistPermissions) {
		logger.info("[bindingRoles]参数:"+userlistPermissions);
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			int deleteCount = userListService.deleteByUserId(userlistPermissions.get(0).getUserlistId());
		int count = userListService.bindingRoles(userlistPermissions);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "添加成功");
		}else {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value = "/updateRoles",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String updateRoles(@RequestBody UserlistPermissions userlistPermissions) {
		logger.info("[updateRoles]参数:"+userlistPermissions);
		Map<String,Object> result = new HashMap<String, Object>();
		try {
		int count = userListService.updateRoles(userlistPermissions);
		if(count > 0) {
			result.put("status", 1001);
			result.put("message", "添加成功");
		}else {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "添加失败");
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/deleteRoles",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String deleteRoles(@RequestBody Map<String,Object> args) {
		logger.info("[deleteRoles]参数:"+args);
		List<UserlistPermissions> userlistPermissions = (List<UserlistPermissions>)args.get("userlistPermissions");
		int size = userlistPermissions.size();
		Map<String,Object> result = new HashMap<String, Object>();
		try {
		int count = userListService.deleteRoles(userlistPermissions);
		int count1 = userListService.bindingRoles(userlistPermissions);
		if(count == size && count1 == size) {
			result.put("status", 1001);
			result.put("message", "删除成功");
		}else {
			result.put("status", 1002);
			result.put("message", "删除失败");
		}
		}catch (Exception e) {
			result.put("status", 1002);
			result.put("message", "删除失败");
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/getUserByName",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getUserByName(@RequestBody Map<String,Object> args) {
		logger.info("[getUserByName]参数:"+args);
		Map<String,Object> result = new HashMap<String, Object>();
		String userName = (String)args.get("userName");
		UserList userList = userListService.getByUserName(userName);
		List<UserList> userLists = new ArrayList<UserList>();
		if(userList != null) {
			List<Long> ids = userListService.getByUserId(userList.getId());
			if(CollectionUtils.isNotEmpty(ids)) {
			userLists = userListService.getUser(ids);
			result.put("status", 1001);
			result.put("message", "查询成功");
			result.put("body", userLists);
			}
			result.put("status", 1001);
			result.put("message", "查询成功");
			result.put("body", userLists);
		}else {
			result.put("status", 1002);
			result.put("message", "账户有误");
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value = "/getUser",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getUser(@RequestBody Map<String,Object> args) {
		logger.info("[getUser]参数:"+args);
		Map<String,Object> result = new HashMap<String, Object>();
		String userName = (String)args.get("userName");
		UserList userList = userListService.getByUserName(userName);
		UserList userListTree = userListService.recursiveTree(userList.getId());
		result.put("status", 1001);
		result.put("message", "查询成功");
		result.put("body", userListTree);
		logger.info("[getUser]返回值:"+userListTree);
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value = "/getRole",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getRole(@RequestBody Map<String,Object> args) {
		logger.info("[getRole]参数:"+args);
		Map<String,Object> result = new HashMap<String, Object>();
		int userId = (int)args.get("userId");
		List<Permissions> roles = userListService.getRoleByUserId(userId);
		result.put("status", 1001);
		result.put("message", "查询成功");
		result.put("body", roles);
		logger.info("[getUser]返回值:"+roles);
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value = "/updateUser",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String updateUser(@RequestBody UserList userList) {
		Map<String,Object> result = new HashMap<String, Object>();
		int count = userListService.updateUser(userList);
		if(count == 1) {
			result.put("status", 1001);
			result.put("message", "修改成功");
		}else {
			result.put("status", 1002);
			result.put("message", "修改失败");
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value = "/deleteUser",method=RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String deleteUser(@RequestBody Map<String,Object> args) {
		Map<String,Object> result = new HashMap<String, Object>();
		List<Long> ids = (List<Long>)args.get("ids");
		int size = ids.size();
		int count = userListService.deleteById(ids);
		if(count == size) {
			result.put("status", 1001);
			result.put("message", "删除成功");
		}else {
			result.put("status", 1002);
			result.put("message", "修改失败");
		}
		return Utility.gson.toJson(result);
	}
}
